home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / tabnanny.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  8KB  |  289 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''The Tab Nanny despises ambiguous indentation.  She knows no mercy.
  5.  
  6. tabnanny -- Detection of ambiguous indentation
  7.  
  8. For the time being this module is intended to be called as a script.
  9. However it is possible to import it into an IDE and use the function
  10. check() described below.
  11.  
  12. Warning: The API provided by this module is likely to change in future
  13. releases; such changes may not be backward compatible.
  14. '''
  15. __version__ = '6'
  16. import os
  17. import sys
  18. import getopt
  19. import tokenize
  20. if not hasattr(tokenize, 'NL'):
  21.     raise ValueError("tokenize.NL doesn't exist -- tokenize module too old")
  22. hasattr(tokenize, 'NL')
  23. __all__ = [
  24.     'check',
  25.     'NannyNag',
  26.     'process_tokens']
  27. verbose = 0
  28. filename_only = 0
  29.  
  30. def errprint(*args):
  31.     sep = ''
  32.     for arg in args:
  33.         sys.stderr.write(sep + str(arg))
  34.         sep = ' '
  35.     
  36.     sys.stderr.write('\n')
  37.  
  38.  
  39. def main():
  40.     global filename_only, verbose
  41.     
  42.     try:
  43.         (opts, args) = getopt.getopt(sys.argv[1:], 'qv')
  44.     except getopt.error:
  45.         msg = None
  46.         errprint(msg)
  47.         return None
  48.  
  49.     for o, a in opts:
  50.         if o == '-q':
  51.             filename_only = filename_only + 1
  52.         
  53.         if o == '-v':
  54.             verbose = verbose + 1
  55.             continue
  56.     
  57.     if not args:
  58.         errprint('Usage:', sys.argv[0], '[-v] file_or_directory ...')
  59.         return None
  60.     for arg in args:
  61.         check(arg)
  62.     
  63.  
  64.  
  65. class NannyNag(Exception):
  66.     '''
  67.     Raised by tokeneater() if detecting an ambiguous indent.
  68.     Captured and handled in check().
  69.     '''
  70.     
  71.     def __init__(self, lineno, msg, line):
  72.         self.lineno = lineno
  73.         self.msg = msg
  74.         self.line = line
  75.  
  76.     
  77.     def get_lineno(self):
  78.         return self.lineno
  79.  
  80.     
  81.     def get_msg(self):
  82.         return self.msg
  83.  
  84.     
  85.     def get_line(self):
  86.         return self.line
  87.  
  88.  
  89.  
  90. def check(file):
  91.     '''check(file_or_dir)
  92.  
  93.     If file_or_dir is a directory and not a symbolic link, then recursively
  94.     descend the directory tree named by file_or_dir, checking all .py files
  95.     along the way. If file_or_dir is an ordinary Python source file, it is
  96.     checked for whitespace related problems. The diagnostic messages are
  97.     written to standard output using the print statement.
  98.     '''
  99.     if os.path.isdir(file) and not os.path.islink(file):
  100.         if verbose:
  101.             print '%r: listing directory' % (file,)
  102.         
  103.         names = os.listdir(file)
  104.         for name in names:
  105.             fullname = os.path.join(file, name)
  106.             if os.path.isdir(fullname) or not os.path.islink(fullname) or os.path.normcase(name[-3:]) == '.py':
  107.                 check(fullname)
  108.                 continue
  109.         
  110.         return None
  111.     
  112.     try:
  113.         f = open(file)
  114.     except IOError:
  115.         not os.path.islink(file)
  116.         msg = not os.path.islink(file)
  117.         errprint('%r: I/O Error: %s' % (file, msg))
  118.         return None
  119.  
  120.     
  121.     try:
  122.         process_tokens(tokenize.generate_tokens(f.readline))
  123.     except tokenize.TokenError:
  124.         None if verbose > 1 else not os.path.islink(file)
  125.         msg = None if verbose > 1 else not os.path.islink(file)
  126.         errprint('%r: Token Error: %s' % (file, msg))
  127.         return None
  128.         except IndentationError:
  129.             msg = None
  130.             errprint('%r: Indentation Error: %s' % (file, msg))
  131.             return None
  132.             except NannyNag:
  133.                 nag = None
  134.                 badline = nag.get_lineno()
  135.                 line = nag.get_line()
  136.                 if verbose:
  137.                     print '%r: *** Line %d: trouble in tab city! ***' % (file, badline)
  138.                     print 'offending line: %r' % (line,)
  139.                     print nag.get_msg()
  140.                 elif ' ' in file:
  141.                     file = '"' + file + '"'
  142.                 
  143.                 if filename_only:
  144.                     print file
  145.                 else:
  146.                     print file, badline, repr(line)
  147.                 return None
  148.             elif verbose:
  149.                 print '%r: Clean bill of health.' % (file,)
  150.             
  151.  
  152.  
  153.  
  154. class Whitespace:
  155.     (S, T) = ' \t'
  156.     
  157.     def __init__(self, ws):
  158.         self.raw = ws
  159.         S = Whitespace.S
  160.         T = Whitespace.T
  161.         count = []
  162.         b = n = nt = 0
  163.         for ch in self.raw:
  164.             if ch == S:
  165.                 n = n + 1
  166.                 b = b + 1
  167.                 continue
  168.             if ch == T:
  169.                 n = n + 1
  170.                 nt = nt + 1
  171.                 if b >= len(count):
  172.                     count = count + [
  173.                         0] * ((b - len(count)) + 1)
  174.                 
  175.                 count[b] = count[b] + 1
  176.                 b = 0
  177.                 continue
  178.             break
  179.         
  180.         self.n = n
  181.         self.nt = nt
  182.         self.norm = (tuple(count), b)
  183.         self.is_simple = len(count) <= 1
  184.  
  185.     
  186.     def longest_run_of_spaces(self):
  187.         (count, trailing) = self.norm
  188.         return max(len(count) - 1, trailing)
  189.  
  190.     
  191.     def indent_level(self, tabsize):
  192.         (count, trailing) = self.norm
  193.         il = 0
  194.         for i in range(tabsize, len(count)):
  195.             il = il + (i / tabsize) * count[i]
  196.         
  197.         return trailing + tabsize * (il + self.nt)
  198.  
  199.     
  200.     def equal(self, other):
  201.         return self.norm == other.norm
  202.  
  203.     
  204.     def not_equal_witness(self, other):
  205.         n = max(self.longest_run_of_spaces(), other.longest_run_of_spaces()) + 1
  206.         a = []
  207.         for ts in range(1, n + 1):
  208.             if self.indent_level(ts) != other.indent_level(ts):
  209.                 a.append((ts, self.indent_level(ts), other.indent_level(ts)))
  210.                 continue
  211.         
  212.         return a
  213.  
  214.     
  215.     def less(self, other):
  216.         if self.n >= other.n:
  217.             return False
  218.         if self.is_simple and other.is_simple:
  219.             return self.nt <= other.nt
  220.         n = max(self.longest_run_of_spaces(), other.longest_run_of_spaces()) + 1
  221.         for ts in range(2, n + 1):
  222.             if self.indent_level(ts) >= other.indent_level(ts):
  223.                 return False
  224.         
  225.         return True
  226.  
  227.     
  228.     def not_less_witness(self, other):
  229.         n = max(self.longest_run_of_spaces(), other.longest_run_of_spaces()) + 1
  230.         a = []
  231.         for ts in range(1, n + 1):
  232.             if self.indent_level(ts) >= other.indent_level(ts):
  233.                 a.append((ts, self.indent_level(ts), other.indent_level(ts)))
  234.                 continue
  235.         
  236.         return a
  237.  
  238.  
  239.  
  240. def format_witnesses(w):
  241.     firsts = map((lambda tup: str(tup[0])), w)
  242.     prefix = 'at tab size'
  243.     if len(w) > 1:
  244.         prefix = prefix + 's'
  245.     
  246.     return prefix + ' ' + ', '.join(firsts)
  247.  
  248.  
  249. def process_tokens(tokens):
  250.     INDENT = tokenize.INDENT
  251.     DEDENT = tokenize.DEDENT
  252.     NEWLINE = tokenize.NEWLINE
  253.     JUNK = (tokenize.COMMENT, tokenize.NL)
  254.     indents = [
  255.         Whitespace('')]
  256.     check_equal = 0
  257.     for type, token, start, end, line in tokens:
  258.         if type == NEWLINE:
  259.             check_equal = 1
  260.             continue
  261.         if type == INDENT:
  262.             check_equal = 0
  263.             thisguy = Whitespace(token)
  264.             if not indents[-1].less(thisguy):
  265.                 witness = indents[-1].not_less_witness(thisguy)
  266.                 msg = 'indent not greater e.g. ' + format_witnesses(witness)
  267.                 raise NannyNag(start[0], msg, line)
  268.             indents[-1].less(thisguy)
  269.             indents.append(thisguy)
  270.             continue
  271.         if type == DEDENT:
  272.             check_equal = 1
  273.             del indents[-1]
  274.             continue
  275.         if check_equal and type not in JUNK:
  276.             check_equal = 0
  277.             thisguy = Whitespace(line)
  278.             if not indents[-1].equal(thisguy):
  279.                 witness = indents[-1].not_equal_witness(thisguy)
  280.                 msg = 'indent not equal e.g. ' + format_witnesses(witness)
  281.                 raise NannyNag(start[0], msg, line)
  282.             indents[-1].equal(thisguy)
  283.             continue
  284.     
  285.  
  286. if __name__ == '__main__':
  287.     main()
  288.  
  289.